package com.sugar.rpc.common.registry;

import com.sugar.rpc.common.annotation.RpcService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

// registry/RpcServiceRegistry.java
@Component
public class RpcServiceRegistry implements ApplicationContextAware {
    private final Map<String, Object> services = new HashMap<>();

    @Override
    public void setApplicationContext(ApplicationContext ctx) {
        Map<String, Object> beans = ctx.getBeansWithAnnotation(RpcService.class);
        for (Object bean : beans.values()) {
            Class<?>[] interfaces = bean.getClass().getInterfaces();
            if (interfaces.length > 0) {
                services.put(interfaces[0].getName(), bean);
            }
        }
    }

    public Object getService(String interfaceName) {
        return services.get(interfaceName);
    }
}
